/**
 * 站点管理服务
 * 基于LeanCloud进行站点数据的CRUD操作
 */

import { dataAPI } from '@/http/leancloud';

export interface StationData {
  objectId?: string;
  code: string;
  name: string;
  type: number; // 0发货站 1中转站 2到达站
  areaCode: string;
  manager: string;
  phone: string;
  address?: string;
  status: number; // 0禁用 1启用
}

export interface StationQueryParams {
  name?: string;
  code?: string;
  type?: number;
  status?: number;
  page?: number;
  pageSize?: number;
}

/**
 * 站点服务类
 */
export class StationService {
  private static readonly CLASS_NAME = 'Station';

  /**
   * 查询站点列表
   */
  static async getStationList(params: StationQueryParams = {}) {
    try {
      const query: any = {};
      
      if (params.name) {
        query.name = { $regex: params.name };
      }
      if (params.code) {
        query.code = { $regex: params.code };
      }
      if (params.type !== undefined) {
        query.type = params.type;
      }
      if (params.status !== undefined) {
        query.status = params.status;
      }

      const data = await dataAPI.query(this.CLASS_NAME, query);
      return {
        success: true,
        data: data.map(this.formatStationData),
        total: data.length
      };
    } catch (error) {
      console.error('查询站点列表失败:', error);
      return { success: false, error, data: [], total: 0 };
    }
  }

  /**
   * 获取启用的站点列表
   */
  static async getActiveStations() {
    return await this.getStationList({ status: 1 });
  }

  /**
   * 创建站点
   */
  static async createStation(stationData: StationData) {
    try {
      const data = await dataAPI.save(this.CLASS_NAME, stationData);
      return { success: true, data };
    } catch (error) {
      console.error('创建站点失败:', error);
      return { success: false, error };
    }
  }

  /**
   * 更新站点
   */
  static async updateStation(stationId: string, updateData: Partial<StationData>) {
    try {
      await dataAPI.save(this.CLASS_NAME, {
        objectId: stationId,
        ...updateData
      });
      return { success: true };
    } catch (error) {
      console.error('更新站点失败:', error);
      return { success: false, error };
    }
  }

  /**
   * 删除站点
   */
  static async deleteStation(stationId: string) {
    try {
      await dataAPI.delete(this.CLASS_NAME, stationId);
      return { success: true };
    } catch (error) {
      console.error('删除站点失败:', error);
      return { success: false, error };
    }
  }

  /**
   * 格式化站点数据
   */
  private static formatStationData(item: any): StationData {
    return {
      objectId: item.objectId,
      code: item.code || '',
      name: item.name || '',
      type: item.type || 0,
      areaCode: item.areaCode || '',
      manager: item.manager || '',
      phone: item.phone || '',
      address: item.address || '',
      status: item.status || 1
    };
  }

  /**
   * 获取站点类型文本
   */
  static getTypeText(type: number): string {
    const typeMap: { [key: number]: string } = {
      0: '发货站',
      1: '中转站',
      2: '到达站'
    };
    return typeMap[type] || '未知类型';
  }

  /**
   * 获取状态文本
   */
  static getStatusText(status: number): string {
    const statusMap: { [key: number]: string } = {
      0: '禁用',
      1: '启用'
    };
    return statusMap[status] || '未知状态';
  }
}